package logic

import (
	"context"
	"fmt"
	"srv/model/mysql"
	"srv/pkg"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 实名认证
func (l *RealNameLogic) RealName(in *srv.RealNameRequest) (*srv.RealNameResponse, error) {
	// todo: add your logic here and delete this line

	r := mysql.RealName{}
	err := r.GetRealName(in.UserId, in.IdCard)
	if err != nil {
		logx.Errorf("查询失败")
		return nil, err
	}
	if r.ID != 0 {
		logx.Errorf("用户身份已存在")
		return nil, err
	}
	if !pkg.RealName(in.IdCard, in.Username) {
		logx.Errorf("实名认证失败")
		return nil, fmt.Errorf("实名认证失败")
	}
	r.UserId = in.UserId
	r.Username = in.Username
	r.IdCard = in.IdCard
	err = r.Create()
	if err != nil {
		logx.Errorf("创建失败")
		return nil, err
	}
	return &srv.RealNameResponse{}, nil
}
